package edu.jptest.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.jptest.bean.ExamineQuestion;
import edu.jptest.util.GetDataBaseConnection;

public class ExamineQuestionDAO {
	// 插入卷题
	public boolean insertExamineQuestion(ExamineQuestion examineQuestion) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "insert into examine_question(question_id,e_p_id,question_pnum) values(?,?,?)";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examineQuestion.getQuestionId());
			pstm.setInt(2, examineQuestion.getExaminePaperId());
			pstm.setInt(3, examineQuestion.getQuestionInPaperNum());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 删除卷题
	public boolean deleteExamineQuestion(int examineQuestion_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "delete from examine_question where examine_question_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examineQuestion_id);
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 更该卷题
	public boolean updateExamineQuestion(ExamineQuestion examineQuestion) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "update examine_question set question_id=?,e_p_id =?,question_pnum=? where examine_question_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examineQuestion.getQuestionId());
			pstm.setInt(2, examineQuestion.getExaminePaperId());
			pstm.setInt(3, examineQuestion.getExamineQuestionId());
			pstm.setInt(4, examineQuestion.getQuestionId());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 通过卷题id，查询卷题对象
	public ExamineQuestion getExamineQuestionByExamineQuestionId(
			int examineQuestion_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		ExamineQuestion examineQuestion = null;
		PreparedStatement pstm = null;
		try {
			String sql = "select * from examine_question where examine_question_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examineQuestion_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examineQuestion.setExamineQuestionId(resultSet.getInt(1));
				examineQuestion.setQuestionId(resultSet.getInt(2));
				examineQuestion.setExaminePaperId(resultSet.getInt(3));
				examineQuestion.setQuestionInPapernum(resultSet.getInt(4));
				return examineQuestion;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return new ExamineQuestion();
	}

	// 通过试卷id，查询该试卷中的卷题id（包括试题及其它关于试题的详细信息）
	public List<Integer> getExamineQuestionsIdByExaminationPaperId(
			int examinationPaper_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> examineQuestionsId = new ArrayList<Integer>();
		try {
			String sql = "select examine_question_id from examine_question where e_p_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examinationPaper_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examineQuestionsId.add(resultSet.getInt(1));
			}
			return examineQuestionsId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过试题在试卷中的编号，获得试题id（）
	public int getQuestionIdByQuestionNumberInExaminePaper(int examinePaperId,
			int questionInPaper_number) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		try {
			String sql = "select question_id from examine_question where e_p_id=? and question_pnum=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, examinePaperId);
			pstm.setInt(2, questionInPaper_number);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				return resultSet.getInt(1);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return 0;
	}

	// 获得所有卷题id
	public List<Integer> getExamineQuestionsId() {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> examineQuestionsId = new ArrayList<Integer>();
		try {
			String sql = "select examine_question_id from examine_question";
			pstm = connection.prepareStatement(sql);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				examineQuestionsId.add(resultSet.getInt(1));
			}
			return examineQuestionsId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}
}
